/*
 * 广州云徙科技有限公司源代码，版权归广州云徙科技有限公司所有。
 * 
 * 项目名称 : yxs-gateway
 * 创建日期 : 2017年3月28日
 * 修改历史 : 
 *     1. [2017年3月28日]创建文件 by tan.wenjuan
 */
package com.project.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.project.common.Constants;
import com.project.ds.ArticleMapper;
import com.project.ds.CategoryMapper;
import com.project.entity.atricle.Article;
import com.project.entity.atricle.Category;
import com.project.entity.enumcommon.LevelEnum;
import com.project.service.IArticleService;

/**
 * @author tan.wenjuan
 */
@Service
public class ArticleServiceImpl implements IArticleService{
    @Autowired
    private ArticleMapper articleMapper;
    
    @Autowired
    private CategoryMapper categoryMapper;
    
    /** 
     * 获取级别为1的所有文章类别
     * @return
     */
    @Cacheable(value="categoryListCache")
    @Override
    public List<Category> getGategoryList(){
        Category category = new Category();
        category.setOrderBy("id");
        category.setLevel(LevelEnum.ONE.getValue());
        return categoryMapper.findList(category);
    }

    /**
     * 根据父ID获取标签列表
     * @param parentId
     * @return
     */
    @Cacheable(value="categoryChildListCache")
    @Override
    public List<Category> getGategoryListByParentId(Long parentId){
        Category category = new Category();
        category.setOrderBy("id");
        category.setParentId(parentId);
        return categoryMapper.findList(category);
    }
    
    /**
     * 根据类别id获取文章列表
     * @param categoryId
     * @return
     */
    @Override
    public List<Article> getArticleListByCategoryId(Long categoryId,int pageSize,int pageNo){
        return articleMapper.getArticleListByCategoryId(categoryId,pageNo,pageSize);
    }

    /**
     * 文章详情
     * @see com.project.service.IArticleService#getArticleDetail(java.lang.Long)
     **/
    @Override
    public Article getArticleDetail(Long id) {
        
        return articleMapper.getById(id);
    }

    /**
     * 判断该文章是否被用户收藏
     * @see com.project.service.IArticleService#isCollection(java.lang.Long, java.lang.Long)
     **/
    @Override
    public boolean isCollection(Long id, Long userId) {
        Map<String,Object> map = articleMapper.getCollectionByUserId(id, userId);
        if(map!=null){
            return true;
        }
        return false;
    }

    /**
     * 根据类别查询文章的封面图（最新五篇）
     * @see com.project.service.IArticleService#getArticlePhotoList(java.lang.Long)
     **/
    @Override
    public List<Map<String,Object>> getArticlePhotoList(Long categoryId) {
        List<Map<String,Object>> list = articleMapper.getArticlePhotoList(categoryId);
        if(!list.isEmpty()){
            for(Map<String,Object> map:list){
                if(map!=null&&map.get("cover_pic_url")!=null){
                    map.put("cover_pic_url", Constants.OBJECT_STORAGE_URL_DOWNLOAD_IMAGE+(String)map.get("cover_pic_url"));
                }
            }
        }
        return list;
    }

    /**
     * 收藏文章
     * @see com.project.service.IArticleService#collectArticle(java.lang.Long, java.lang.Long)
     **/
    @Override
    public void collectArticle(Long articleId, Long userId) {
        articleMapper.collectArticle(articleId, userId);
    }

    /**
     * 取消收藏
     * @see com.project.service.IArticleService#cancelCollect(java.lang.Long, java.lang.Long)
     **/
    @Override
    public void cancelCollect(Long articleId, Long userId) {
        articleMapper.cancelCollect(articleId, userId);
    }

    /**
     * 更新文章的浏览量，每进一次详情加1
     * @see com.project.service.IArticleService#updatePageView(java.lang.Long)
     **/
    @Override
    public void updatePageView(Long id) {
        articleMapper.updatePageView(id);
        
    }

    /**
     * 用户收藏列表
     * @see com.project.service.IArticleService#collectList(java.lang.Long)
     **/
    @Override
    public List<Article> collectList(Long userId,int pageSize,int pageNo) {
        return articleMapper.collectList(userId, pageSize, pageNo);
    }

    /**
     * 标题名查询文章列表
     * @see com.project.service.IArticleService#getArticleListByTitle(java.lang.String, int, int)
     **/
    @Override
    public List<Article> getArticleListByTitle(String title, int pageSize, int pageNo) {
        return articleMapper.getArticleListByTitle(title, pageSize, pageNo);
    }

    /**
     * 收藏的文章数量
     * @see com.project.service.IArticleService#collectCount(java.lang.Long)
     **/
    @Override
    public int collectCount(Long userId) {
        return articleMapper.collectCount(userId);
    }

    /**
     * 搜索数量
     * @see com.project.service.IArticleService#articleCount(java.lang.String)
     **/
    @Override
    public int articleCount(String title) {
        return articleMapper.articleCount(title);
    }

    /**
     * 根据标签id获取文章列表
     * @see com.project.service.IArticleService#getArticleListByLabelId(java.lang.Long, int, int)
     **/
    @Override
    public List<Article> getArticleListByLabelId(Long labelId, int pageSize, int pageNo) {
        return articleMapper.getArticleListByLabelId(labelId,pageNo,pageSize);
    }

    /**
     * 根据标签查询文章的封面图（最新五篇）
     * @see com.project.service.IArticleService#getArticlePhotoListByLabelId(java.lang.Long)
     **/
    @Override
    public List<Map<String, Object>> getArticlePhotoListByLabelId(Long labelId) {
        List<Map<String,Object>> list = articleMapper.getArticlePhotoListByLabelId(labelId);
        if(!list.isEmpty()){
            for(Map<String,Object> map:list){
                if(map!=null&&map.get("cover_pic_url")!=null){
                    map.put("cover_pic_url", Constants.OBJECT_STORAGE_URL_DOWNLOAD_IMAGE+(String)map.get("cover_pic_url"));
                }
            }
        }
        return list;
    }
    
    
}
